home *** CD-ROM | disk | FTP | other *** search
Text File | 1989-10-06 | 12.3 KB | 452 lines | [TEXT/MPS ] |
- {************************************************************************************
- *
- * Project Name: FTTools
- * File Name: ftutil.p
- * Authors: Rob Neville, Alex Kazim, Carol Lee, Byron Han
- * Date: May 17, 1989
- *
- * Description:
- *
- *************************************************************************************
- *
- * Revision History:
- * 5/17/89 - Original version by Rob Neville
- * 6/26/89 - Rev'd for b2 of Comm Toolbox
- * 7/24/89 - Rev'd for b4 of Comm Toolbox
- *
- ************************************************************************************}
-
-
- UNIT FTUtil;
-
- INTERFACE
-
- USES
- MemTypes, QuickDraw, OSIntf, ToolIntf, PackIntf,
- FixMath, Script,ConnectionTool,CRMIntf;
-
- CONST
- Toke_Max = 256; { max # of tokens }
- String_Space = 4096; { space for strings }
- WhiteTokens = [tokenWhite,tokenNewLine,tokenNoBreakSpace];
- NumberTokens = [tokenNumeric,tokenAltNum,tokenRealNum,tokenAltReal];
- Res_ID_Limit = 100;
-
-
-
- function InitTokenBlock(var aTokenPtr:TokenBlockPtr): OSErr;
- procedure DisposeTokenBlock(aTokenPtr: TokenBlockPtr);
- function MatchResString(aString: str255;start,finish,StrResID:integer): integer;
- function GetSuperToken(aTokenPtr: TokenBlockPtr; var offset: integer; var theString: Str255): integer;
- function StrLen(theString:Ptr):longint;
- function TranslateConfig(procID: integer; inputStr: Ptr; var outputStr: Ptr; fromLanguage,toLanguage: longint;toolClass: ResType): longint;
- function MyCat(var toPtr:Ptr;theString:Str255;calStrSize:Boolean): longint;
-
- IMPLEMENTATION
-
-
- { ******************************** }
- { Returns the length of a c-string }
- { ******************************** }
-
- function strLen(theString:Ptr):longint;
- var
- endPtr : Ptr;
-
- begin
- endPtr:= theString;
- while endPtr^ <> 0 do {scan until we find \0 termination}
- endPtr:= ptr(ord(endPtr) + 1);
- strLen:= ord4(endPtr) - ord4(theString);
- end;
-
-
-
- { ******************************************** }
- { initialize the token block for tokenize call }
- { ******************************************** }
-
- function InitTokenBlock(var aTokenPtr:TokenBlockPtr): OSErr;
- CONST
- Toke_Max = 256; { max # of tokens }
- String_Space = 4096; { space for strings }
- VAR
- Itl4 : Itl4Handle; { the itl4 resource }
-
- BEGIN
- Itl4 := Itl4Handle(IUGetIntl(4)); { Get the 'itl4' resource }
-
- if (Itl4 = nil) then begin { Which way did he go? }
- InitTokenBlock := ResError;
- EXIT(InitTokenBlock);
- end;
-
- HLock(Handle(Itl4)); { Give Blood }
-
- { gimme space }
- aTokenPtr := TokenBlockPtr(NewPtr(sizeof(TokenBlock)));
- if (aTokenPtr = nil) then begin
- InitTokenBlock := MemError;
- EXIT(InitTokenBlock);
- end;
-
- { gimme more }
- aTokenPtr^.tokenList := NewPtr(sizeof(TokenRec) * Toke_Max);
- if (aTokenPtr^.tokenList = nil) then begin
- DisposPtr(Ptr(aTokenPtr));
- InitTokenBlock := MemError;
- EXIT(InitTokenBlock);
- end;
-
- { # of tokens to allow }
- aTokenPtr^.tokenLength := Toke_Max;
- aTokenPtr^.tokenCount := 0;
-
- { build strings of each token }
- aTokenPtr^.stringList := NewPtr(String_Space);
- if (aTokenPtr^.stringList = nil) then begin
- DisposPtr(aTokenPtr^.tokenList);
- DisposPtr(Ptr(aTokenPtr));
- InitTokenBlock := MemError;
- EXIT(InitTokenBlock);
- end;
-
- aTokenPtr^.stringLength := String_Space;
- aTokenPtr^.stringCount := 0;
-
- { set the controls to the heart of the sun }
- aTokenPtr^.doString := true;
- aTokenPtr^.doAppend := false;
- aTokenPtr^.doAlphanumeric := true;
- aTokenPtr^.doNest := false;
-
- { parser info for double quotes }
- aTokenPtr^.leftDelims[0] := token2Quote;
- aTokenPtr^.leftDelims[1] := DelimPad;
-
- aTokenPtr^.rightDelims[0] := token2Quote;
- aTokenPtr^.rightDelims[1] := DelimPad;
-
- { parser info for comments: unused }
- aTokenPtr^.leftComment[0] := DelimPad;
- aTokenPtr^.leftComment[1] := DelimPad;
- aTokenPtr^.leftComment[2] := DelimPad;
- aTokenPtr^.leftComment[3] := DelimPad;
-
- aTokenPtr^.rightComment[0] := DelimPad;
- aTokenPtr^.rightComment[1] := DelimPad;
- aTokenPtr^.rightComment[2] := DelimPad;
- aTokenPtr^.rightComment[3] := DelimPad;
-
- { "\"" will generate a token2Quote }
- aTokenPtr^.escapeCode := tokenBackSlash;
- aTokenPtr^.decimalCode := tokenPeriod;
- aTokenPtr^.itlResource := Handle(Itl4);
- aTokenPtr^.reserved[0] := 0;
- aTokenPtr^.reserved[1] := 0;
- aTokenPtr^.reserved[2] := 0;
- aTokenPtr^.reserved[3] := 0;
- aTokenPtr^.reserved[4] := 0;
- aTokenPtr^.reserved[5] := 0;
- aTokenPtr^.reserved[6] := 0;
- aTokenPtr^.reserved[7] := 0;
-
- HUnlock(Handle(Itl4));
-
- InitTokenBlock := noErr;
- END;
-
- procedure DisposeTokenBlock(aTokenPtr: TokenBlockPtr);
- BEGIN
- if (aTokenPtr <> nil) then begin
- if (aTokenPtr^.stringList <> nil) then
- DisposPtr(aTokenPtr^.stringList);
-
- if (aTokenPtr^.TokenList <> nil) then
- DisposPtr(Ptr(aTokenPtr^.TokenList));
-
- DisposPtr(Ptr(aTokenPtr)); {clean it up, boys}
- end;
- END;
-
- FUNCTION GetSuperToken(aTokenPtr: TokenBlockPtr; var offset: INTEGER;
- var theString: Str255): INTEGER;
- VAR
- myToken : TokenRecPtr;
-
- BEGIN
- theString := '';
-
- myToken := TokenRecPtr(ORD4(aTokenPtr^.tokenList) +
- offset * sizeof(TokenRec));
-
- { if it's a white token then return that }
- if (myToken^.theToken in WhiteTokens) then begin
- offset := offset + 1;
- theString := myToken^.stringPosition^;
- GetSuperToken := tokenWhite;
- end
- else if myToken^.theToken = tokenLeftLit then begin
-
- { if tokenLeftLit, walk 'till tokenRightLit }
- { this way we get escape chars as well }
-
- { get the next token }
- offset := offset + 1;
- myToken := TokenRecPtr(ORD4(aTokenPtr^.tokenList) +
- offset * sizeof(TokenRec));
-
- { walk 'till right lit }
- while (myToken^.theToken <> tokenRightLit) and
- (offset < aTokenPtr^.tokenCount) do begin
-
- { Do I need to interpret \n as newline, etc ???}
-
- { eat the escape, but print the rest }
- if (myToken^.theToken <> tokenEscape) then
- theString := concat(theString,myToken^.stringPosition^);
-
- offset := offset + 1;
- myToken := TokenRecPtr(ORD4(aTokenPtr^.tokenList) +
- offset * sizeof(TokenRec));
- end; { while }
-
- offset := offset + 1; { get rid of right quote }
-
- GetSuperToken := tokenAlpha;
-
- end
- else begin { start concating them until next white token }
-
- GetSuperToken := myToken^.theToken;
-
- { walk 'till next white space }
- while not (myToken^.theToken in WhiteTokens) and
- (offset < aTokenPtr^.tokenCount) do begin
- theString := concat(theString,myToken^.stringPosition^);
-
- offset := offset + 1;
- myToken := TokenRecPtr(ORD4(aTokenPtr^.tokenList) +
- offset * sizeof(TokenRec));
- end; { while }
-
- end; { else }
- END;
-
-
-
- { ************************************************************ }
- { Searches for a match between passed string and str# resource }
- { specified by StrResID and returns the index of the string in }
- { str# resource }
- { ************************************************************ }
-
- function MatchResString(aString: str255;start,finish,StrResID:integer): integer;
- const
- string_not_found = -1;
- var
- i : integer;
- resStr : str255;
-
- begin
- for i := start to finish do
- begin
- GetIndString(resStr,StrResID,i);
- if EqualString(resStr,aString,false,false) then
- begin
- MatchResString := i;
- EXIT(MatchResString);
- end;
- end;
- MatchResString := string_not_found;
- end; {MatchResString}
-
-
-
- { **************************************************************** }
- { Concatenates a Str255 onto the end of a zero-terminated c string }
- { Returns any memory allocation errors }
- { **************************************************************** }
-
- function MyCat(var toPtr:Ptr;theString:Str255;calStrSize:Boolean): longint;
- VAR
- stringlen : longint;
-
- BEGIN
- stringlen := length(theString);
-
- if not calStrSize then begin
- BlockMove(Ptr(ORD4(@theString)+1),toPtr,stringlen);
-
- toPtr := Ptr(ORD4(toPtr) + stringlen);
- toPtr^ := ord(' ');
-
- toPtr := Ptr(ORD4(toPtr) + 1); { advance the ptr and leave }
- end;
-
- MyCat := stringlen + 1; { size + 1 }
- END; {MyCat}
-
- FUNCTION TranslateConfig(procID: INTEGER; inputStr: Ptr; var outputStr: Ptr;
- fromLanguage,toLanguage: LONGINT;
- toolClass: ResType): LONGINT;
- TYPE
- IntPtr = ^INTEGER;
-
- VAR
- returnVal : INTEGER; {any Errors encountered}
- aTokenPtr : TokenBlockPtr;
- tokeStr : Str255;
- alienStr : Str255;
- savedoutputStr : Ptr ;
- localOutputStr : Ptr ;
- myToken : TokenRecPtr;
- i : INTEGER; {loop counter}
- totalLen : LONGINT;
- notDone : Boolean ;
- firstPass : Boolean ;
- stringlen : INTEGER ;
- resIndexList : PACKED ARRAY[1..Toke_Max] OF INTEGER;
- maxString : INTEGER;
- resH : Handle;
- oldResRef : INTEGER;
- offset : INTEGER;
-
- BEGIN
- outputStr := nil ; { set outputStr to zero in case of error }
- {Map resources to local IDs}
-
- if (fromLanguage > Res_ID_Limit) or
- (toLanguage > Res_ID_Limit) then begin
- TranslateConfig := 0 ;
- EXIT( TranslateConfig ) ;
- end;
-
- fromLanguage:= CRMLocalToRealID(toolClass, procID,'STR#',fromLanguage);
- toLanguage:= CRMLocalToRealID(toolClass, procID,'STR#',toLanguage);
-
- oldResRef := CurResFile; { Put us at the forefront }
- UseResFile(procID);
-
- resH := Get1Resource('STR#',fromLanguage);
- if resH <> nil then
- maxString := IntPtr(resH^)^
- else
- maxString := 0;
-
- UseResFile(oldResRef);
-
- notDone := TRUE ;
- firstPass := TRUE ;
- if (fromLanguage = -1) or (toLanguage = -1) then begin
- { language not found in resource }
- TranslateConfig := 0 ;
- EXIT( TranslateConfig ) ;
- end;
-
- returnVal:= InitTokenBlock(aTokenPtr);
- if returnVal <> noErr then begin
- TranslateConfig:= returnVal;
- EXIT(TranslateConfig); {abort, abort}
- end;
-
- returnVal := 0 ; { init returnVal for later use }
- aTokenPtr^.source := inputStr; { what to parse }
- aTokenPtr^.sourceLength := StrLen(inputStr); { just how long }
-
- {tokenize the string}
- if IntlTokenize(aTokenPtr) <> tokenOK then begin
- DisposeTokenBlock(aTokenPtr);
- TranslateConfig:= -1; { Unknown error }
- EXIT(TranslateConfig); { Warp Factor 8 }
- end;
-
- oldResRef := CurResFile;
- UseResFile(procID);
-
- { we have to visit the tokens twice because the length of the outstr }
- { has to be figured out and memory space has to allocated before }
- { we actually copy anything to outstr }
- while notDone do begin
-
- offset := 0; { offset into the tokenBlock }
-
- for i:= 1 to aTokenPtr^.tokenCount do begin
-
- myToken := TokenRecPtr(ORD4(aTokenPtr^.tokenList) +
- offset * sizeof(TokenRec));
- tokeStr := '';
-
- { Translate only Alpha tokens }
- if (myToken^.theToken = tokenAlpha) then begin
-
- { walk 'till next white space }
- while not (myToken^.theToken in WhiteTokens) and
- (offset < aTokenPtr^.tokenCount) do begin
- tokeStr := concat(tokeStr,myToken^.stringPosition^);
-
- offset := offset + 1;
- myToken := TokenRecPtr(ORD4(aTokenPtr^.tokenList) +
- offset * sizeof(TokenRec));
- end; { while }
-
- { find in the from language }
- if firstPass then
- resIndexList[i] :=
- MatchResString(tokeStr,1,maxString,fromLanguage);
-
- { get the equivalent from the to language }
- if resIndexList[i] <> -1 then begin
- GetIndString(alienStr,toLanguage,resIndexList[i]);
- if length(alienStr) > 0 then
- tokeStr := alienStr;
- end;
-
- end {tokenAlpha}
- else begin { just copy other token }
-
- offset := offset + 1;
- tokeStr := myToken^.stringPosition^;
-
- end; {token type? }
-
- stringlen := length(tokeStr);
-
- if firstPass then { we just want the length in the first pass }
- returnVal := returnVal + stringlen
- else begin { copy to the output string }
- BlockMove(Ptr(ORD4(@tokeStr) + 1), localOutputStr, stringlen);
- localOutputStr := Ptr(ORD4(localOutputStr) + stringlen);
- end;
-
- if (offset >= aTokenPtr^.tokenCount) then
- leave;
- end; {for every token}
-
- if firstPass then begin
- firstPass := FALSE ;
- localOutputStr := NewPtr(returnVal+1) ; { extra one for null terminator }
- if localOutputStr = nil then begin
- TranslateConfig := MemError ;
- DisposeTokenBlock(aTokenPtr);
- EXIT(TranslateConfig);
- end
- else
- { save a copy of the pointer, since we'll be advancing localOutputStr }
- savedoutputStr := localOutputStr ;
- end
- else
- notDone := FALSE; { I'm all done with it }
- end; { while }
-
- UseResFile(oldResRef);
-
- localOutPutStr^ := 0 ; { null terminate the returned C-string }
- outputStr := savedoutputStr;
-
- DisposeTokenBlock(aTokenPtr);
- TranslateConfig := 0 ;
- END; {TranslateConfig}
-
-
- END.